package q2d.client.controllers;

import q2d.client.worlds.GameWorld;
import q2d.common.CollisionDetector;
import q2d.common.models.units.Bullet;
import q2d.common.models.units.Player;

import com.brackeen.javagamebook.input.InputManager;

/** BulletsController
 * Kontroler pociskow gracza tej instancji gry. Odpowiada za ich ruch i kolizje ze scianami.
 *
 */
public class BulletsController extends Controller {


	// referencja swiat a gry:
	private GameWorld gameWorld;
	// referencja do obiektu InputManagera
	protected InputManager playerInput;
	// referencja do FXcontroller'a
	private FXController fxController;
	// detektor kolizji
	private CollisionDetector collisionDetector;

	private int uidCoutner = 0;// TODO wywalic to i zrobic Singletona z generatorem UID

	/**
	 * Konstruktor.
	 * @param input_manager
	 * @param game_world
	 * @param game_width
	 * @param game_height
	 */
	public BulletsController(int game_width, int game_height, InputManager input_manager, GameWorld game_world, FXController fx_controller){
		super(game_width, game_height);
		playerInput = input_manager;
		gameWorld = game_world;
		fxController = fx_controller;

		collisionDetector = new CollisionDetector();
	}

	@Override
	public void controll() {
		if (!isWorking) return;

		// iteracja po wszystkich pociskach
		for (int i=0; i<gameWorld.thisBulletsList.size(); i++){
			Bullet b = gameWorld.thisBulletsList.get(i);

			// detekcja kolizji ze sciana
			float collisionPoint = collisionDetector.isBulletMapCollision(gameWorld.gameMap, b.x, b.y, b.vx, b.vy); 
			if (collisionPoint < 0){
				b.x += b.vx;
				b.y += b.vy;				
			} else {
				fxController.addWallHitExplosion((int)(b.x + b.vx * collisionPoint), (int)(b.y + b.vy * collisionPoint), b.vx, b.vy);
				gameWorld.thisBulletsList.remove(i);
				i--;
			}

			// detekcja kolizji z obcymi graczami
//			for (int j=0; j<gameWorld.playersList.size(); j++){
//				Player p = gameWorld.playersList.get(j);
//				if (collisionDetector.isBulletPlayerCollision(b.x, b.y, b.vx, b.vy, p.x, p.y)){
//					gameWorld.thisBulletsList.remove(i);
//					i--;
//					j = gameWorld.playersList.size();
//				}
//			}
		}
	}

	@Override
	public void disableControll() {
		if (!isWorking) return;
		isWorking = false;

		playerInput.resetAllGameActions();
	}

	@Override
	public void enableControll() {
		if (isWorking) return;
		isWorking = true;
	}


	/**
	 * Dodaje pocisk danego typu. 
	 * @param x
	 * @param y
	 * @param vx
	 * @param vy
	 * @param type
	 */
	public void addBullet(double x, double y, double vx, double vy, int type){
		Bullet bullet = new Bullet();
		bullet.x = x;
		bullet.y = y;
		bullet.clientID = 0;
		bullet.unitID = uidCoutner++;
		double scale;

		switch (type){
			case Bullet.TYPE_1: 
				scale = (Bullet.TYPE_1_SPEED / Math.sqrt(vx*vx+vy*vy));
				bullet.vx = vx * scale;
				bullet.vy = vy * scale;
				break;
		}

		gameWorld.addThisBullet(bullet);
		
	}
}
